package y24.m1.homework;

public class Test8 {
    public static void main(String[] args) {
        String input = "pidancode.com";
        // 计算字符平均数
        double average = getAverage(input);
        System.out.println("字符平均数：" + average);
        // 计算字符方差
        double variance = getVariance(input, average);
        System.out.println("字符方差：" + variance);
    }
    // 计算字符平均数
    private static double getAverage(String input) {
        int sum = 0;
        for (int i = 0; i < input.length(); i++) {
            sum += (int) input.charAt(i);
        }
        return (double) sum / input.length();
    }
    // 计算字符方差
    private static double getVariance(String input, double average) {
        double sumSquares = 0;
        for (int i = 0; i < input.length(); i++) {
            int diff = (int) input.charAt(i) - (int) average;
            sumSquares += Math.pow(diff, 2);
        }
        return sumSquares / input.length();
    }
}
